home *** CD-ROM | disk | FTP | other *** search
- /*
-
- LINKED LIST
-
- Write a program similar exactly the same as queue except the following:
-
- the ships will be stored in a circular linked list (see the structure below)
-
- typedef struct ship_type
- {
- int hull_number;
- struct ship_type * next;
- } ship_type;
-
- A pointer tail will point to the last ship in the list. Each ship points to
- the next in the list using the pointer next variable. The last element of the
- list points to the head of the list. When a ship arrives, it is added to the
- end of the list. When one is serviced, it is removed from the head of the list.
-
- There is no size limit.
- Assume that any memory allocation is successful (do not check malloc)
- Make sure that the memory for an element if freed when it is removed from the list
-
- Use the following procedures:
- simulation as in queue.c
- add_on add a ship to the linked list
- take_off remove a ship from the list
- display_list display the hull numbers in the list from head to tail
-
- please note: special conditions will have to be made for adding a ship when
- the list is empty, and removing a ship when only 1 ship is in the list.
-
- */
-
-
- /* ------------------------------------------------------------------------------- */
-
-
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
-
-
- #define MAX_ARRIVAL 12 /* maximum arrival interval */
- #define MAX_SERVICE 24 /* maximum service interval */
- #define TIME_LIMIT 100 /* run of simulation (0 to TIME_LIMIT) */
- #define random(x) (rand() % (x))
-
-
- typedef struct ship_type
- {
- int hull_number;
- struct ship_type * next;
- } ship_type;
-
-
- static void simulation (void); /* called by main TIME_LIMIT times */
- static void add_on (void); /* adds ship to linked list */
- static void take_off (void); /* removes by changing head */
- static void display_list (void); /* lists ships in linked list, followed by newline */
-
-
- static ship_type * tail = NULL;
- static int clock; /* clock goes from 0 to TIME_LIMIT */
- static int arrival; /* next arrival interval */
- static int service; /* next service interval */
- static FILE * file_stream; /* file control block pointer */
- static int new_number = 101; /* hull numbers for newly created ships */
-
-
- /* ------------------------------------------------------------------------------- */
-
-
- main()
- {
- file_stream = fopen("ship list.out", "w");
- arrival = random(MAX_ARRIVAL);
- service = random(MAX_SERVICE);
- for (clock = 0; clock < TIME_LIMIT; clock++)
- simulation();
- fclose(file_stream);
- }
-
-
- /* ------------------------------------------------------------------------------- */
-
-
- static void simulation (void)
- {
- if (!arrival--) /* check if an arrival occurred and decrement arrival */
- {
- add_on(); /* if arrival, add ship */
- while (!(arrival = random(MAX_ARRIVAL)))
- add_on(); /* loop while arrival time is 0 */
- display_list();
- }
- if (!service--) /* check if service and decrement service */
- {
- take_off(); /* if service, remove ship */
- while (!(service = random(MAX_SERVICE)))
- take_off(); /* loop while service time is 0 */
- }
- }
-
-
- /* ------------------------------------------------------------------------------- */
-
-
- static void add_on(void)
- {
- ship_type *new_ship;
-
- new_ship = (ship_type *) malloc(1 * sizeof(ship_type)); /* allocate memory for ship */
- new_ship->hull_number = new_number++; /* create ship number */
- if (!tail) /* if linked list is empty */
- {
- tail = new_ship; /* set first element in linked list */
- tail->next = tail; /* to point to itself */
- }
- else /* if at least 1 element on linked list */
- {
- new_ship->next = tail->next; /* newest ship points to head */
- tail->next = new_ship; /* last element points to newest element */
- tail = new_ship; /* tail pointer now points to newest element */
- } /* since this is now the last element */
- }
-
-
- /* ------------------------------------------------------------------------------- */
-
-
- static void take_off(void)
- {
- ship_type * delete;
-
- if (!tail) /* if no elements, do nothing */
- return;
- if (tail->next == tail) /* if only 1 element in list */
- {
- free(tail); /* release memory for the element */
- tail = NULL; /* force pointer to point to NULL */
- }
- else /* if more than 1 element */
- {
- delete = tail->next; /* temp pointer points to first element */
- tail->next = tail->next->next; /* last element points to 2nd element */
- free(delete); /* free memory used by first element */
- } /* now, second element is first element */
- }
-
-
- /* ------------------------------------------------------------------------------- */
-
-
- static void display_list(void)
- {
- ship_type * list;
-
- list = tail->next; /* looping pointer points to head of linked list */
- while (list != tail) /* do until the pointer reaches the last element */
- {
- fprintf(file_stream, "%4d\n", list->hull_number); /* print element hull number */
- list = list->next; /* have pointer point to next element */
- }
- fprintf(file_stream, "%4d\n\n", list->hull_number); /* print last element hull number */
- } /* followed by an extra return */
-